package com.zhentao.dataintegrity.scheduler;

import com.zhentao.dataintegrity.service.IntegrityTaskService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 数据完整性检查定时任务调度器
 */
@Component
public class IntegrityTaskScheduler {

    private static final Logger log = LoggerFactory.getLogger(IntegrityTaskScheduler.class);

    @Autowired
    private IntegrityTaskService taskService;

    /**
     * 每分钟扫描一次需要执行的定时任务
     */
    @Scheduled(cron = "0 * * * * ?")
    public void scanScheduledTasks() {
        try {
            log.debug("开始扫描定时任务...");
            taskService.executeScheduledTasks();
            log.debug("定时任务扫描完成");
        } catch (Exception e) {
            log.error("定时任务扫描失败", e);
        }
    }

    /**
     * 每天凌晨3点清理30天前的历史记录
     */
    @Scheduled(cron = "0 0 3 * * ?")
    public void cleanHistoryData() {
        try {
            log.info("开始清理历史数据...");
            // 这里可以调用清理服务
            log.info("历史数据清理完成");
        } catch (Exception e) {
            log.error("清理历史数据失败", e);
        }
    }
}
